/**
 * Created by hss on 2017/4/19.
 */

var httpPost = function(requestPath, requestJson, successResult, errorResult) {
    console.log("url:" + requestPath);
    console.log("params:" + JSON.stringify(requestJson));
    $.ajax({
        url: requestPath,
        data: requestJson,
        type: "POST",
        dataType: "JSON",
        contentType: "application/x-www-form-urlencoded; charset=utf-8",
        success: function(data, textStatus) {
            console.log("result:" + JSON.stringify(data));
            if (data.status == "1") {
                successResult(data.data);
            } else {
                errorResult(data.msg);
            }
        },
        error: function(xhr, textStatus, errorThrown) {
            console.log("httpPost:" +
                "\nurl:" + requestPath +
                "\nerror:" + xhr.state);
            //errorResult();
        }
    });
};

var httpPost_async = function(requestPath, requestJson, successResult, errorResult) {
    console.log("url:" + requestPath);
    console.log("params:" + JSON.stringify(requestJson));
    $.ajax({
        url: requestPath,
        data: requestJson,
        type: "POST",
        async: false,
        dataType: "JSON",
        contentType: "application/x-www-form-urlencoded; charset=utf-8",
        success: function(data, textStatus) {
            console.log("result:" + JSON.stringify(data));
            if (data.status == "1") {
                successResult(data.data);
            } else {
                errorResult(data.msg);
            }
        },
        error: function(xhr, textStatus, errorThrown) {
            console.log("httpPost:" +
                "\nurl:" + requestPath +
                "\nerror:" + xhr.state);
            errorResult();
        }
    });
};


var httpGet = function(requestPath, successResult, errorResult) {
    console.log("url:" + requestPath);
    $.ajax({
        url: requestPath,
        data: null,
        type: "GET",
        dataType: "json",
        contentType: "application/x-www-form-urlencoded; charset=utf-8",
        success: function(data, textStatus) {
            console.log("result:" + JSON.stringify(data));
            if (data.status == "1") {
                successResult(data.data);
            } else {
                errorResult(data.msg);
            }
        },
        error: function(xhr, textStatus, errorThrown) {
            console.log("httpGet:" +
                "\nurl:" + requestPath +
                "\nerror:" + xhr.state);
            var msg;
            if (xhr.state === 408) {
                msg = "连接超时";
            } else if (xhr.state === 404) {
                msg = "连接失败";
            } else if (xhr.state === 500) {
                msg = "服务器错误";
            }
            errorResult(msg);
        }
    });
};

function getUrlParam(name) {
    var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)"); //构造一个含有目标参数的正则表达式对象
    var r = window.location.search.substr(1).match(reg); //匹配目标参数
    if (r != null) return unescape(r[2]);
    return null; //返回参数值
}

function showDialog(message) {
    $.Huimodalalert(message, 2000);
}

function toHtml(path) {
    window.location.href = path;
}

/******************************************************************************************************************/
function getSignString(jsapi_ticket, noncestr, timestamp, url) {
    var str = "";
    str += "jsapi_ticket=" + jsapi_ticket + "&";
    str += "noncestr=" + noncestr + "&";
    str += "timestamp=" + timestamp + "&";
    str += "url=" + url;
    return str;
}

// 获取长度为len的随机字符串
function _getRandomString(len) {
    len = len || 32;
    var $chars = 'ABCDEFGHJKMNPQRSTWXYZabcdefhijkmnprstwxyz2345678'; // 默认去掉了容易混淆的字符oOLl,9gq,Vv,Uu,I1
    var maxPos = $chars.length;
    var pwd = '';
    for (var i = 0; i < len; i++) {
        pwd += $chars.charAt(Math.floor(Math.random() * maxPos));
    }
    return pwd;
}

function isObjNull(obj) {
    return obj == undefined || obj == "" || obj == NaN || obj == "undefined";
}

function getStoreId() {
    return 1;
}

function getNowFormatDate() {
    var date = new Date();
    var seperator1 = "-";
    var year = date.getFullYear();
    var month = date.getMonth() + 1;
    var strDate = date.getDate();
    if (month >= 1 && month <= 9) {
        month = "0" + month;
    }
    if (strDate >= 0 && strDate <= 9) {
        strDate = "0" + strDate;
    }
    var currentdate = year + seperator1 + month + seperator1 + strDate
    return currentdate;
}

function getStoreId() {
    return JSON.parse(window.localStorage.getItem("manager")).sId;
}

function setUserinfo(userinfo) {
    window.localStorage.setItem("manager", JSON.stringify(userinfo));
}

function getUserinfo() {
    return JSON.parse(window.localStorage.getItem("manager"));
}

function getUserid() {
    return JSON.parse(window.localStorage.getItem("manager")).id;
}

function logout() {
    window.localStorage.clear();
    window.location.href = "login.html";
}

function timestampToTime(timestamp) {
    var date = new Date(timestamp); //时间戳为10位需*1000，时间戳为13位的话不需乘1000
    var Y = date.getFullYear() + '-';
    var M = (date.getMonth() + 1 < 10 ? '0' + (date.getMonth() + 1) : date.getMonth() + 1) + '-';
    var D = date.getDate() < 10 ? '0' + date.getDate() : date.getDate() + ' ';
    var h = date.getHours() < 10 ? '0' + date.getHours() : date.getHours();
    var m = date.getMinutes() < 10 ? '0' + date.getMinutes() : date.getMinutes();
    var s = date.getSeconds() < 10 ? '0' + date.getSeconds() : date.getSeconds();
    return Y + M + D +" "+ h + ':'+ m + ':'+ s;
}


function toDateStr(timestamp) {
    var date = new Date(timestamp); //时间戳为10位需*1000，时间戳为13位的话不需乘1000
    var Y = date.getFullYear() + '-';
    var M = (date.getMonth() + 1 < 10 ? '0' + (date.getMonth() + 1) : date.getMonth() + 1) + '-';
    var D = date.getDate() < 10 ? '0' + date.getDate() : date.getDate();
    console.log("date:" + Y + M + D);
    return Y + M + D ;
}